package cn.nubia.admin.sysconfig.controller;

import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.springmvc.Response;
import cn.nubia.admin.common.springmvc.WebContext;
import cn.nubia.admin.sysconfig.bean.Role;
import cn.nubia.admin.sysconfig.bean.RoleUser;
import cn.nubia.admin.sysconfig.service.RoleService;
import cn.nubia.admin.sysconfig.service.RoleUserService;
import cn.nubia.admin.sysconfig.bean.User;
import cn.nubia.admin.sysconfig.query.UserQuery;
import cn.nubia.admin.sysconfig.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 用户操作界面控制层.
 */
@Controller
@RequestMapping("/user/")
@RequiresPermissions("sys:syscfg:user:list")
public class UserController {


    @Autowired
    private UserService userService;

    @Autowired
    private RoleUserService roleUserService;

    @Autowired
    private RoleService roleService;


    @RequestMapping(value = "userList")
    public ModelAndView getUserList(UserQuery userQuery) {
        ModelAndView modelAndView = new ModelAndView("/user/userList");
        Paged<User> userList = userService.findPage(userQuery);
        modelAndView.addObject("userList", userList);
        modelAndView.addObject("query", userQuery);
        return modelAndView;
    }

//    @RequiresPermissions("user:add")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public Response save(User user) {
        userService.save(user);
        return Response.getSuccessResponse();
    }

//    @RequiresPermissions("user:list")
    @RequestMapping(value = "view", method = RequestMethod.GET)
    public ModelAndView view(Integer userId) {
        ModelAndView modelAndView = new ModelAndView("/user/userForm");
        if(userId==null){
            modelAndView = new ModelAndView("/user/myInfo");
        }
        User user = new User();
        userId = userId==null?WebContext.getCachedUserId():userId;
        if (userId != null) {
            user = userService.get(userId);
            Role role = roleUserService.getRoleByUserId(userId);
            List<Role> roleList = roleService.findAll();
            modelAndView.addObject("roleList", roleList);
            modelAndView.addObject("userRole", role);
        }

        modelAndView.addObject("user", user);
        modelAndView.addObject("userId", userId);
        return modelAndView;
    }

//    @RequiresPermissions("user:role")
    @RequestMapping(value = "viewRole", method = RequestMethod.GET)
    public ModelAndView viewRole(Integer userId) {
        ModelAndView modelAndView = new ModelAndView("/user/userRoleForm");
        User user = new User();
        if (userId != null) {
            user = userService.get(userId);
            Role role = roleUserService.getRoleByUserId(userId);
            List<Role> roleList = roleService.findAll();
            modelAndView.addObject("roleList", roleList);
            modelAndView.addObject("roleId", role.getId());
        }

        modelAndView.addObject("user", user);
        modelAndView.addObject("userId", userId);
        return modelAndView;
    }
//    @RequiresPermissions("user:resource")
    @RequestMapping(value = "viewResource", method = RequestMethod.GET)
    public ModelAndView viewResource(Integer userId) {
        ModelAndView modelAndView = new ModelAndView("/user/userResourceForm");
        User user = new User();
        if (userId != null) {
            user = userService.get(userId);
            Role role = roleUserService.getRoleByUserId(userId);
            List<Role> roleList = roleService.findAll();
            modelAndView.addObject("roleList", roleList);
            modelAndView.addObject("roleId", role.getId());
        }

        modelAndView.addObject("user", user);
        modelAndView.addObject("userId", userId);
        return modelAndView;
    }

//    @RequiresPermissions("user:role")
    @RequestMapping(value = "saveUserRole", method = RequestMethod.POST)
    @ResponseBody
    public Response saveUserRole(RoleUser roleUser) {
        roleUserService.save(roleUser);
        return Response.getSuccessResponse();
    }

//    @RequiresPermissions("user:del")
    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public String delete(Integer userId) {
        userService.delete(userId);
        return "redirect:userList.html";
    }

}
